<template>

    <el-dialog
        :visible.sync="isToggleStartOrStopDiaVisible"
        width="30%"
        :before-close="hideToggleStartOrStopDiaVisible"
        center>
        <div>
            <el-form :model="form" :rules="rules" ref="form" label-width="100px">
                <el-form-item label="暂停原因" prop="stopReason" v-if = "toggleStr === '暂停'">
                    <el-input v-model.trim = "form.stopReason" placeholder="请输入暂停原因" maxlength='100' class="inputTxtWrapper"></el-input>
                </el-form-item>
                <el-form-item :label="toggleStr + '时间'" prop="time">
                    <el-date-picker
                        style = "width:100%"
                        v-model="form.time"
                        type="date"
                        class="inputTxtWrapper"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择时间">
                    </el-date-picker>
                </el-form-item>
            </el-form>
            <!-- <p class="tipRed">暂停需要填写原因</p> -->
        </div>
        
        <span slot="footer" class="dialog-footer">
            <el-button @click="hideToggleStartOrStopDiaVisible">取 消</el-button>
            <el-button type="primary" @click="toggleStartOrStop">确 定</el-button>
        </span>
    </el-dialog>

</template>

<script>
import http from '@/assets/js/http'
import _g from '@/assets/js/global'

export default {
    mixins: [http],
    props:{
        toggleStr : {
            type : String,
            required : true
        },
        isToggleStartOrStopDiaVisible : {
            type : Boolean,
            required:true,
            default:false
        },
        proAnalyId:{
            type:Object,
            required : true
        },
        proAnalyAuditCount:{
            type:Number,
            required : true
        },
    },
    data(){
        return{
            form : {
                time : moment().format('YYYY-MM-DD'),//..暂停 / 启动 / 完成时间，默认当前时间
                stopReason : ''///暂停原因
            },
            rules : {
                stopReason : [{
                    required : true,message : '请输入暂停原因',trigger : 'blur'
                }],
                time :  [{
                    required : true,message : '请选择暂停时间',trigger : 'blur'
                }]
            }
        }
    },
    methods: {
        /* 关闭弹框 */
        hideToggleStartOrStopDiaVisible(str) {
            this.form.stopReason = ''
            this.form.time = moment().format('YYYY-MM-DD HH:mm:ss')
            this.$emit('hideToggleStartOrStopDiaVisible',str)
        },
        /* 暂停立项分析 */
        toggleStartOrStop(){
            const that = this
            const stopReason = that.form.stopReason// 暂停原因
            const time = that.form.time// 暂停 / 启动 / 完成时间，默认当前时间
            const proAnalyId = that.proAnalyId

            const proAnalyAuditCount = that.proAnalyAuditCount

            let data = {}
            let url = 'admin/task/toggleStartOrStop'
            if(that.toggleStr === '暂停'){
                data = {proAnalyId,typeStr:'暂停',stopReason,time,proAnalyAuditCount}
            }else if(that.toggleStr === '启动'){
                data = {proAnalyId,typeStr:'启动',time,proAnalyAuditCount}
            }else if(that.toggleStr === '完成'){
                data = {proAnalyId,time,proAnalyAuditCount}
                url = 'admin/task/sureProAnaly'
            }

            // if(stopReason === ''){
            //     return
            // }



            that.$refs.form.validate((valid) => {
                if(valid){
                    that.$confirm('是否确认提交?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        center: true
                    }).then(() => {
                            _g.showLoading('提交中')
                            that.apiPost(url, data).then((res) => {
                                _g.showLoading().close()
                                if(typeof res === 'string'){
                                    res = JSON.parse(res)
                                }
                                if (res.code != 200) {
                                    that.handleError(res)
                                } else {
                                    if(res.data.isSuccess === 0){             //  成功
                                        _g.toastMsg("success", res.message)

                                        that.hideToggleStartOrStopDiaVisible('立项分析暂停或启动或完成成功')

                                    }else if(res.data.isSuccess === 1){
                                        _g.toastMsg("error", res.message)
                                    }else if(res.data.isSuccess === 2){
                                        _g.toastMsg("warning", res.message)
                                    }
                                }
                            })
                    }).catch(() => {})
                }
            })
        },
    },
}
</script>

<style scope="less">
</style>

